/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.PresentationLayer;

/// <summary>
/// Summary description for Class1
/// </summary>
namespace EXtremecode.PresentationLayer
{
    public class AWRequiredFieldValidator : RequiredFieldValidator
    {
        
        public override Control FindControl(string id)
        {
            Control ctrl = base.FindControl(id);
            if (ctrl is IASCXControl)
            {
                return ((IASCXControl)ctrl).InputControl; 
            }
            return ctrl;
        }

        protected override bool ControlPropertiesValid()
        {
            
            return true;
        }

        protected override bool EvaluateIsValid()
        {
            bool isValid = false;
            string firstValue = string.Empty;

            //firstValue
            Control ctrlConrolToValidate = FindControl(ControlToValidate);
            if (ctrlConrolToValidate is TextBox)
            {
                firstValue = ((TextBox)ctrlConrolToValidate).Text;
            }
            else if (ctrlConrolToValidate is DropDownList)
            {
                firstValue = ((DropDownList)ctrlConrolToValidate).SelectedValue;
            }

            isValid = firstValue != string.Empty;
            return isValid;
        }
 
    }
    public class AWCustomValidator : CustomValidator
    {
        public override Control FindControl(string id)
        {
            Control ctrl = base.FindControl(id);
            if (ctrl is IASCXControl)
            {
                return ((IASCXControl)ctrl).InputControl;
            }
            return ctrl;
        }
        protected override bool ControlPropertiesValid()
        {
            return true;
        }
        

        private string controlToCompare = string.Empty;
        public string ControlToCompare
        {
            get
            {
                return controlToCompare; 
            }
            set
            {
                controlToCompare = value;
            }
        }
    }

    public class AWCompareValidator : CompareValidator 
    {

        public override Control FindControl(string id)
        {
            Control ctrl = base.FindControl(id);
            if (ctrl is IASCXControl)
            {
                return ((IASCXControl)ctrl).InputControl;
            }
            return ctrl;
        }

        protected override bool ControlPropertiesValid()
        {

            return true;
        }

        protected override bool EvaluateIsValid()
        {
            bool isValid = false;
            string firstValue = string.Empty;
            string secondValue = string.Empty;

            //firstValue
            Control ctrlConrolToValidate = FindControl(ControlToValidate);
            if (ctrlConrolToValidate is TextBox)
            {
                firstValue = ((TextBox)ctrlConrolToValidate).Text;
            }
            else if (ctrlConrolToValidate is DropDownList)
            {
                firstValue = ((DropDownList)ctrlConrolToValidate).SelectedValue;
            }

            //secondValue
            Control ctrlConrolToCompare;
            if (string.IsNullOrEmpty(ControlToCompare))
            {
                secondValue = ValueToCompare;
            }
            else
            {
                ctrlConrolToCompare = FindControl(ControlToCompare);
                if (ctrlConrolToCompare is TextBox)
                {
                    firstValue = ((TextBox)ctrlConrolToCompare).Text;
                }
                else if (ctrlConrolToCompare is DropDownList)
                {
                    firstValue = ((DropDownList)ctrlConrolToCompare).SelectedValue;
                }
            }

            isValid =firstValue == secondValue;

            return isValid;
        }

    }
}
